package com.anycc.cloud.core.controller;

import com.anycc.cloud.common.dto.datatable.DataTableRequest;
import com.anycc.cloud.common.dto.datatable.DataTableResponse;
import com.anycc.cloud.common.dto.easyui.Tree;
import com.anycc.cloud.common.dto.json.ListResponse;
import com.anycc.cloud.common.dto.json.ObjectResponse;
import com.anycc.cloud.common.dto.json.Response;
import com.anycc.cloud.common.dto.json.SuccessResponse;
import com.anycc.cloud.common.dto.query.OrderBy;
import com.anycc.cloud.core.entity.Area;
import com.anycc.cloud.core.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 区划控制器
 */
@RestController
@RequestMapping("area")
public class AreaController {
    @Autowired
    private AreaService areaService;


    /**
     * 创建区划
     *
     * @param area 区划
     * @return response
     */
    @PostMapping("create")
    @ResponseBody
    public Response create(Area area) {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        areaService.create(area);
        return new ObjectResponse<Integer>(request.getServerPort());
    }

    /**
     * 更新区划
     *
     * @param area 区划
     * @return Response
     */
    @PostMapping("update")
    public Response update(Area area) {
        areaService.update(area);
        return new SuccessResponse();
    }

    /**
     * 删除区划
     *
     * @param id 区划id
     * @return Response
     */
    @PostMapping("delete")
    public Response delete(@RequestParam("id") String id) {
        this.areaService.delete(id);
        return new SuccessResponse();
    }

    /**
     * build area tree
     *
     * @return tree
     */
    @GetMapping("tree")
    public Response tree() {
        return new ListResponse<>(areaService.tree());
    }

    /**
     * build parent area tree
     *
     * @return tree
     */
    @GetMapping("tree/parent")
    public List<Tree> parentTree(@RequestParam("id") String id) {
        return areaService.parentTree(id);
    }

    /**
     * datatable paging query
     *
     * @param area    area
     * @param request DataTableRequest
     * @return DataTableResponse
     */
    @GetMapping("findByPaging")
    public DataTableResponse<Area> findByPaging(Area area, DataTableRequest request) {
        List<OrderBy> orders = new ArrayList<>();
        orders.add(new OrderBy("desc", "createTime"));
        return new DataTableResponse<Area>(areaService.findByPaging(area, request.buildPageRequest(orders)), request);
    }

    /**
     * find area by id
     *
     * @param id id
     * @return Area
     */
    @GetMapping("findById")
    public Area findById(@RequestParam("id") String id) {
        return areaService.findById(id);
    }


}
